
/**
 * Created by guowl on 17/10/30.
 */
'use strict';

angular.module('backofficeApp.commissionSettlementManager.controller', [])
    .controller('commissionSettlementCtrl', ['$scope','$http','$routeParams','$location', function ($scope, $http,$routeParams,$location) {


        $scope.currentPage = 1;
        $scope.pageSize = 15;


       //分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/commission/commissionSettlement/:page/:size',
            ajax: $http,
            success:function(data){
                if(data && data.data) {
                    $scope.mtOrderList = data.data.list;
                    $scope.totalInfo = data.data.totalInfo;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.currentPage = $(".am-active").text();
                    $scope.pagination.totalPage = data.data.totalPage;
                }
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                $scope.mtOrderList = null;
            },
            params: {
                initFlg :true}
        });

        //搜索
        $scope.search = function (current) {
            if (current) {
                $scope.currentPage = 1;
            }else {
                $scope.currentPage = 1;
                $scope.currentPage = 1;
            }

            var startDateSelect = $("#startDateSelect").val();
            if(startDateSelect == '') {
                alert('开始日期不能为空！');
                return;
            }
            var endDateSelect = $("#endDateSelect").val();
            if(endDateSelect == '') {
                alert('结束日期不能为空！');
                return;
            }
            if(startDateSelect > endDateSelect) {
                alert('开始日期不能大于结束日期！');
                return;
            }
            var nickName = $("#nickName").val();
/*            var mobilePhone = $("#mobilePhone").val();
            var userId = $("userId").val();*/
            var opt = {
                startdate:startDateSelect,
                enddate:endDateSelect,
                nickName : nickName
            };
            // if(nickName != '') {
            //     opt = {
            //         startdate:startDateSelect,
            //         enddate:endDateSelect,
            //         nickName:nickName,
            //     };
            // }

            //if ($scope.status) opt.status = $scope.status;
            $(".alert").css("display","block");
            $http.post('http://' + backOfficeHost + '/rest/v1.0/commission/commissionSettlement/' + $scope.currentPage  + '/' + $scope.pageSize, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $(".alert").css("display","none");
                if (data && data.data) {
                    $scope.mtOrderList = data.data.list;
                    $scope.totalInfo = data.data.totalInfo;
                    $scope.pageNumber = data.data.totalPage;
                    $scope.count = data.data.count;
                    $scope.currentPage = $(".am-active").text();
                    $scope.pagination.totalPage = data.data.totalPage;
                    $scope.pagination.pageHeader($scope.pageNumber);
                    <!--  宝宝树66小区三次改造    mod   guo.xiaomin  20171017  start   -->
                    // 追加,解决分页搜索问题
                    $scope.pagination.searchParams = opt;
                } else {
                    $scope.mtOrderList = [];
                    $scope.totalInfo = {};
                    $scope.pageNumber = 0;
                    $scope.count = 0;
                    $scope.currentPage = $(".am-active").text();
                    $scope.pagination.totalPage = 0;
                    $scope.pagination.pageHeader($scope.pageNumber);
                    <!--  宝宝树66小区三次改造    mod   guo.xiaomin  20171017  start   -->
                    // 追加,解决分页搜索问题
                    $scope.pagination.searchParams = opt;
                }

                <!--  宝宝树66小区三次改造    mod   guo.xiaomin  20171017  end   -->
                $('#loading').modal("close");
            }).error(function (err) {
                $('#loading').modal("close");
                $(".alert").css("display","none");
                alert(err);
            });
        };

        $scope.exportdata = function (option, target) {
            //$scope.pagination.currentPage = 1;
            //$scope.currentPage = 1;
            var startDateSelect = $("#startDateSelect").val();
            var endDateSelect = $("#endDateSelect").val();
            var userInfo = $("#nickName").val();
            var opt = {
                startDate:startDateSelect,
                endDate:endDateSelect
            };
            var href = 'http://' + backOfficeHost + '/rest/v1.0/commission/exportOrderCommission';
/*            if ($("#startDateSelect").val())  href += "&startDate=" + $("#startDateSelect").val() + ' 00:00:00';
            if ($("#endDateSelect").val())  href += "&endDate=" + $("#endDateSelect").val() + ' 23:59:59';*/
            if(!$("#startDateSelect").val() && !$("#endDateSelect").val()){
                alert('请选择日期');
                return;
            }

            if ($("#startDateSelect").val() && $("#endDateSelect").val() && $("#startDateSelect").val() >$("#endDateSelect").val()) {
                alert('开始日期不能大于结束日期');
                return;
            }
            //console.debug(href);
            $(".alert").css("display","block");
            location.href = href+"?startDate="+startDateSelect+"&endDate="+endDateSelect+"&userInfo="+userInfo;
            $(".alert").css("display","none");
        };

        $scope.selectExcelFile = function (files) {

            if (!FileReader) alert("浏览器暂时不支持FileReader");
            $scope.excel = files[0];
        };

        // 导入小区
        $scope.import = function() {
            var formData = new FormData();
            if (!($scope.excel&&$scope.excel.name.length>5&&$scope.excel.name.substring($scope.excel.name.length-4,$scope.excel.name.length)=="xlsx")) {
                alert("未选择Excel文件！");
                $('#doc-modal-import').modal("close");
                $('#loading').modal("close");
                $('#doc-modal-import').modal("close");
                return;
            }

            formData.append('excel', $scope.excel);
            $http.post('http://' + backOfficeHost + '/rest/v1.0/commission/commissionImport', formData, {
                transformRequest: angular.identity, headers: {'Content-Type': undefined}
            }).success(function (data) {
                if(data.result && data.result.status==0){
                    $('#doc-modal-import').modal("close");
                    $('#loading').modal("close");
                    $('#doc-modal-import').modal("close");
                    alert("更新数据失败，导入文件没有订单id或者订单中含有未精算的数据。");
                }else{
                    $('#doc-modal-import').modal("close");
                    $('#loading').modal("close");
                    $('#doc-modal-import').modal("close");
                    alert("导入数据成功");
                }
            }).error(function () {
                $('#doc-modal-import').modal("close");
                $('#loading').modal("close");
                $('#doc-modal-import').modal("close");
                alert("导入数据失败！");
            });
        };


    }]);


